import os
import time
import ddddocr
import pyperclip
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
from selenium import webdriver
from selenium.webdriver.common.by import By

from config import FMT_PATH, BASE_DIR

store_9528 = "http://cymtest.piccmall.cn:9528/login"
store_9529 = "http://cymtest.piccmall.cn:9529/login"
store_9531 = "http://cymtest.piccmall.cn:9531/login"
urllist = [store_9528, store_9529, store_9531]

# driver = webdriver.Chrome(executable_path=r"F:\work\driver\chrome\113\chromedriver.exe")
driver = webdriver.Firefox()
driver.maximize_window()
driver.get(store_9528)
handle_9528 = driver.current_window_handle
time.sleep(1)
pic_root = os.path.abspath(os.path.dirname(os.getcwd()))  # 得到当前根目录的 上一级目录
image_file = os.path.join(pic_root, 'image')  # 新建一个文件夹 ，和当前项目同级目录
if not os.path.exists(image_file):
    os.makedirs(image_file)

img_base = os.path.join(image_file, f"验证码{FMT_PATH}.png")
# 右键保存图片
codeimg = driver.find_element_by_xpath('//*[@id="app"]//div[2]/img')  # 获取元素
action = ActionChains(driver).move_to_element(codeimg)  # 移动到该元素
# -----------------------------------------------------------------------------
action.context_click(codeimg)  # 右键点击该元素
action.perform()  # 执行
pyautogui.typewrite(['v'])  # 敲击V进行保存
# 单击图片另存之后等1s敲回车
pyperclip.copy(img_base)  # 把 指定的路径拷贝到过来
time.sleep(2)
pyautogui.hotkey('ctrlleft', 'v')  # 粘贴
time.sleep(0.5)  # 等待一秒
pyautogui.press('enter')
time.sleep(0.5)  # 等待一秒

ocr = ddddocr.DdddOcr()
with open(img_base, 'rb') as f:  # 打开图片
    img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print(res)  # 打印图片
# print(driver.find_element_by_tag_name('input').get_attribute('class'))
driver.find_element_by_xpath('//*[@placeholder="请输入账号/手机号" and @class= "el-input__inner"]').send_keys(13000000003)
driver.find_element_by_xpath('//*[@placeholder="请输入密码"]').send_keys(123456)
driver.find_element_by_xpath('//*[@placeholder="请输入图形验证码"]').send_keys(res)
driver.find_element_by_xpath('//*/div/button/span').click()
time.sleep(3)
# pyautogui.hotkey('ctrlleft', 't')
# driver.get("http://cymtest.piccmall.cn:9529/login")
handles = []
for i in range(1, 3):
    driver.execute_script(f'window.open("{urllist[i]}")')  # 打开新窗口
    # 在各个标签页打开URL
    # windows = driver.window_handles  # 获取所有窗口
    driver.switch_to.window(driver.window_handles[i])  # 切换到第二个窗口
    h = driver.current_window_handle
    handles.append(h)
all_h = driver.window_handles


for i in all_h:
    if handles[1] == i:
        driver.switch_to.window(i)
time.sleep(4)
print(driver.current_url)
driver.find_element(By.XPATH, '//*[@id="app"]/div/div/div/div[1]/div[2]/div/div').click()
time.sleep(3)
driver.find_element(By.CSS_SELECTOR, 'input.weui-input.searchInput1').send_keys("诸葛亮")

driver.find_element(By.CSS_SELECTOR, 'span.taro-text.iconfont.icon-cart').click()  #  添加购物车
driver.execute_script("document.body.style.zoom='0.5'")
time.sleep(2)
driver.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div/div[3]/div[2]/div[2]'
                              '/div[2]/div/div/div/div/div[2]').click()  # 同意协议
time.sleep(17)
js = 'return document.getElementsByClassName("at-submit-btn")[0].click();'
driver.execute_script(js)
